/**
 * 构建分页标签体的类
 */
package com.brilliance.project.common.tag;

import java.io.Writer;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.components.Component;

import com.brilliance.project.common.Constants;
import com.opensymphony.xwork2.util.ValueStack;

public class Pages extends Component {
	private HttpServletRequest request;
	// 当前页码
	private String curPageNo = null;
	// 当前页码的hidden变量
	private String curPageNoHidParam = null;
	// 总页数
	private String total = null;
	// 总笔数
	private String totalRecord = null;
	// 分页的样式
	private String styleClass = null;
	// action的路径
	private String url = null;
	// action的方法
	private String method = null;
	// actionName
	private String action = null;

	// ---------setter & getter start---------
	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String getCurPageNo() {
		return curPageNo;
	}

	public void setCurPageNo(String curPageNo) {
		this.curPageNo = curPageNo;
	}

	public String getTotal() {
		return total;
	}

	public void setTotal(String total) {
		this.total = total;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTotalRecord() {
		return totalRecord;
	}

	public void setTotalRecord(String totalRecord) {
		this.totalRecord = totalRecord;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	// ---------setter & getter end---------

	// ---------constructor start---------
	public Pages(ValueStack vs, HttpServletRequest request) {
		super(vs);
		this.request = request;
	}

	// ---------constructor end---------

	@Override
	public boolean start(Writer writer) {
		boolean result = super.start(writer);
		try {
			boolean isValid = true;
			StringBuilder str = new StringBuilder();
			str.append("<div class='paginationDiv'><span ");
			if (this.styleClass != null) {
				str.append("class=");
				str.append(this.styleClass);
			}
			str.append("> ");
			// 从ValueStack中取出数值
			// 获得当前页数
			if (isValid) {
				if (this.curPageNo.startsWith("%{")
						&& this.curPageNo.endsWith("}")) {
					this.curPageNoHidParam = this.getHiddenParamName(curPageNo);
					this.curPageNo = getString(this.curPageNo);
					isValid = this.curPageNo == null ? false : true;
				} else {
					isValid = false;
				}
			}

			// 获得总笔数
			if (isValid) {
				if (this.totalRecord.startsWith("%{")
						&& this.totalRecord.endsWith("}")) {
					this.totalRecord = getString(this.totalRecord);
					isValid = this.totalRecord == null ? false : true;
				} else {
					isValid = false;
				}
			}

			// 获得总页数
			if (isValid) {
				if (this.total.startsWith("%{") && this.total.endsWith("}")) {
					this.total = getString(this.total);
					isValid = this.total == null ? false : true;
				} else {
					isValid = false;
				}
			}

			if (isValid) {
				Integer totalInt = Integer.valueOf(this.total);

				if (totalInt > 0) {
					// [首页]
					if ("1".equals(this.curPageNo)) {
						str.append("<span>[首页]</span>");
					} else {
						str.append("<span style=\"cursor: hand;color:#006699;\" onclick=\"go('1', '"
								+ this.curPageNoHidParam
								+ "','"
								+ this.action
								+ "','"
								+ this.method
								+ "')\" "
								+ "onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\""
								+ ">[首页]</span>");
					}
					str.append("&nbsp;&nbsp;&nbsp;&nbsp;");

					// [上一页]
					if ("1".equals(this.curPageNo)) {
						str.append("<span>[上一页]</span>");
					} else {
						String prePage = "";
						prePage = (Integer.parseInt(this.curPageNo) - 1) + "";
						str.append("<span style=\"cursor: hand;color:#006699;\" onclick=\"go('"
								+ prePage
								+ "', '"
								+ this.curPageNoHidParam
								+ "','"
								+ this.action
								+ "','"
								+ this.method
								+ "')\""
								+ "onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\""
								+ ">[上一页]</span>");
					}
					str.append("&nbsp;&nbsp;&nbsp;&nbsp;");
					// [下一页]
					if (this.curPageNo.equals(this.total)) {
						str.append("<span>[下一页]</span>");
					} else {
						String sufPage = "";
						sufPage = (Integer.parseInt(this.curPageNo) + 1) + "";
						str.append("<span style=\"cursor: hand;color:#006699;\" onclick=\"go('"
								+ sufPage
								+ "', '"
								+ this.curPageNoHidParam
								+ "','"
								+ this.action
								+ "','"
								+ this.method
								+ "')\""
								+ "onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\""
								+ ">[下一页]</span>");
					}
					str.append("&nbsp;&nbsp;&nbsp;&nbsp;");
					// [ 尾页]
					if (this.curPageNo.equals(this.total)) {
						str.append("<span>[尾页]</span>");
					} else {
						str.append("<span style=\"cursor: hand;color:#006699;\" onclick=\"go('"
								+ totalInt
								+ "', '"
								+ this.curPageNoHidParam
								+ "','"
								+ this.action
								+ "','"
								+ this.method
								+ "')\""
								+ "onmouseover=\"this.style.textDecoration='underline'\" onmouseout=\"this.style.textDecoration='none'\""
								+ ">[尾页]</span>");
					}
					str.append("&nbsp;&nbsp;&nbsp;&nbsp;");
					// 页面选择的下拉bar
					str.append("跳到第<select name=\"changePage\" onchange=\"go(this.value, '"
							+ this.curPageNoHidParam
							+ "','"
							+ this.action
							+ "','" + this.method + "')\">");
					for (int tempInt = 1; tempInt <= totalInt; tempInt++) {
						if (Integer.parseInt(this.curPageNo) == tempInt) {
							str.append("<option value='" + tempInt
									+ "' selected>" + tempInt + "</option>");
						} else {
							str.append("<option value='" + tempInt + "'>"
									+ tempInt + "</option>");
						}

					}
					str.append("</select>页");
				}

				// 3/3 页，每页 5 条，共有 13 条记录
				str.append("&nbsp;&nbsp;&nbsp;&nbsp;" + this.curPageNo + "/"
						+ this.total + " 页，每页 "
						+ Constants.DEFAULT_MAX_PAGE_RECORD + " 条，共有 "
						+ this.totalRecord + " 条记录");
				str.append("</span></div>");
			}

			writer.write(str.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * description 处理后的字符串
	 * 
	 * @param src
	 *            需处理符串
	 * @return 字符串
	 */
	private String getString(String src) {
		String rtn = "";
		try {
			if (src != null && !"".equals(src)) {
				src = src.substring(2, src.length() - 1);
				rtn = (String) this.getStack().findValue(src);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}

	/**
	 * description:获取到画面上分页hidden变量名称
	 * 
	 * @param src
	 *            需处理的字符串
	 * @return 字符串
	 */
	private String getHiddenParamName(String src) {
		String rtn = "";
		try {
			if (src != null && !"".equals(src)) {
				rtn = src.substring(2, src.length() - 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rtn;
	}
}
